//
//  ECGChartView.m
//  Ikinloop-demo
//
//  Created by Li Shen on 2021/4/22.
//  Copyright © 2021 lyh. All rights reserved.
//

#import "ECGChartView.h"
#import "DrawLiveEcgMgr.h"


@interface ECGChartView()

@property (nonatomic, strong) DrawLiveEcgMgr *drawEcgMgr;
@property (nonatomic, assign) CGFloat beginPointX;
@property (nonatomic, assign) CGFloat endPointX;
@property (nonatomic, assign) CGFloat dataStartX;


@property (nonatomic, copy)NSArray *ecgDatas;
@property (nonatomic, copy)NSArray *ecgStates;

@end

@implementation ECGChartView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self setupDefaultValues];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
        [self setupDefaultValues];
    }
    return self;
}

- (void)setupDefaultValues{
    self.enableScroll = false;
    self.beginPointX = 0;
    self.endPointX = 0;
    self.dataStartX = 0;
}

- (void)setupUI{
    self.clipsToBounds  =true;
    self.drawEcgMgr = [[DrawLiveEcgMgr alloc] initWithLayer:self.layer];
}

-(void)IKEcgDrawinit{
    [self.drawEcgMgr drawBackgroundGrid];
}

- (void)reload{
    [self.drawEcgMgr reload];
}

-(void)IKECGDrawUninit{
    [self.drawEcgMgr reset];
}


-(void)IKECGDrawData:(int)data{
    [self.drawEcgMgr drawWaveByRepeat:data];
}

-(void)IKECGDrawDatas:(NSArray <NSNumber *> *)datas{
    for (NSNumber *dataValue in datas) {
        [self.drawEcgMgr drawWaveByRepeat:dataValue.intValue];
    }
}

- (void)IKECGDrawWithEcgData:(NSArray*)ecgDatas ecgStatas:(NSArray*)ecgStates{
    self.ecgDatas = ecgDatas;
    self.ecgStates = ecgStates;
    [self drawEcg:0];
}


- (void)setEcgDatas:(NSArray *)ecgDatas{
    _ecgDatas = ecgDatas;
    [self drawEcg:0];
}

- (void)drawEcg:(int)dataStartX{
    int linePointNum = [self.drawEcgMgr getPointMaxNumPerScreen];
    NSArray *ecgdatas = [self getEcgDataList:dataStartX count:linePointNum];
    NSArray *ecgstates = [self getEcgStateList:dataStartX count:linePointNum];
    [self.drawEcgMgr drawLineEcg:ecgdatas ecgStates:ecgstates];
}

- (void)clickChartView:(CGPoint)point{
    [self.drawEcgMgr getEcgStateByXY:point];
}

- (void)moveEcgData:(CGFloat)endX{
    CGFloat distance = endX - self.beginPointX;
    CGFloat xStep = [self.drawEcgMgr getxStepPixelPerPoint];
    int xPoints = (int)(distance/xStep);
    self.dataStartX = self.dataStartX - xPoints;
    
    int linePointNum = [self.drawEcgMgr getPointMaxNumPerScreen];
    int length = (int)self.ecgDatas.count;
    if(length - self.dataStartX <= linePointNum){
        self.dataStartX = length - linePointNum;
    }
    if(self.dataStartX <= 0){
        self.dataStartX = 0;
    }
    [self drawEcg:(int)self.dataStartX];
    self.beginPointX = endX;
}

- (NSArray*)getEcgDataList:(CGFloat)start count:(CGFloat)count{
    int size = count > (self.ecgDatas.count - start) ?(self.ecgDatas.count - start) :count;
    NSArray *ecgdatas = [self.ecgDatas subarrayWithRange:NSMakeRange(start, size)];
    
    return ecgdatas;
}

- (NSArray*)getEcgStateList:(CGFloat)start count:(CGFloat)count{
    int size = count > (self.ecgStates.count - start) ?(self.ecgStates.count - start) :count;
    NSArray *ecgdatas = [self.ecgStates subarrayWithRange:NSMakeRange(start, size)];
    
    return ecgdatas;
}

#pragma mark
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    self.beginPointX = touchPoint.x;
    [self clickChartView:touchPoint];
    NSLog(@"touchesBegan--%@",NSStringFromCGPoint(touchPoint));
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.beginPointX = 0;
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    [self moveEcgData:touchPoint.x];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return  NO;
}

- (void)setSmallgridColor:(UIColor *)smallgridColor{
    _smallgridColor = smallgridColor;
    self.drawEcgMgr.smallgridColor = _smallgridColor;
}

- (void)setBgColor:(UIColor *)bgColor{
    _bgColor = bgColor;
    self.drawEcgMgr.bgColor = bgColor;
}

- (void)setGridColor:(UIColor *)gridColor{
    _gridColor = gridColor;
    self.drawEcgMgr.gridColor = gridColor;
}

- (void)setEcgColor:(UIColor *)ecgColor{
    _ecgColor = ecgColor;
    self.drawEcgMgr.ecgColor = ecgColor;
}

- (void)setEcgLineWidth:(CGFloat)ecgLineWidth{
    _ecgLineWidth = ecgLineWidth;
    self.drawEcgMgr.ecgLineWidth = ecgLineWidth;
}

- (void)setSmallGridLineWidth:(CGFloat)smallGridLineWidth{
    _smallGridLineWidth = smallGridLineWidth;
    self.drawEcgMgr.smallGridLineWidth = smallGridLineWidth;
}

- (void)setGridLineWidth:(CGFloat)gridLineWidth{
    _gridLineWidth = gridLineWidth;
    self.drawEcgMgr.gridLineWidth = gridLineWidth;
}

@end
